<?php
/*
#########################FeedCreator 1.0#############################################
Class Name: FeedCreate and FeedItem
Description: This class is used to create the RSS2.0 Feed 
Author: Suraj Thapaliya
Email: suraj@syber.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; 



#########################FeedCreator 1.0#############################################
*/
class FeedCreate{
	
	public $encoding		=	"ISO-8859-1";
	public $application		=	"text/xml";
	public $xsl				=	"";
	public $gmt				=	"+01:00";
	public $rssFormat		=	"2.0";
	public $items 			= 	array();
	public $title			=	"";
	public $link			=	"";
	public $description		=	"";
	public $image_data		=	"";
	public $language		=	"en-us";
	
	protected $generator	=	"FeedCreator 1.0";
	protected $comment		=	"<!-- Feed Build By FeedCreator 1.0 visit http://surajthapaliya.com.np for further information. -->\n";
	
	function __construct(){
		switch($this->rssFormat) {
			case "RSS2.0":
				header('Content-type: text/xml', true);
			break;
		}
	}

	function add_header($title,$link,$description) {
		$this->title=$title;
		$this->description=$description;
		$this->link=$link;
	}
	
	function get_date($feed_date=""){
		if($feed_date=="")
			$feed_date=time();
		else 
			$feed_date=$feed_date;
			
		$date = gmdate("D, d M Y H:i:s",$feed_date);		
		if ($this->gmt!="") $date .= " ".str_replace(":","",$this->gmt);
		return $date;
	}
	
	
	function create_image($title="",$img_path="",$link="") {
		$image_output="";
		$image_output.="      <image>\n";
	    	$image_output.="          <title>".$title."</title>\n";
        	$image_output.="          <url>".$img_path."</url>\n";
	    	$image_output.="          <link>".$link."</link>\n";
    	$image_output.="      </image>\n";
		
		$this->image_data=$image_output;
	}
	
	function add_items($items){
		$this->items[]=$items;
	}
	
	function redirect($filename){
		header("Content-Type: ".$this->application."; charset=".$this->encoding."; filename=".basename($filename));
		header("Content-Disposition: inline; filename=".basename($filename));
		readfile($filename, "r");
		die();

	}
		
	
	function create_feed(){
		$output="";
		$output.="<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
		$output.=$this->comment;
		if($this->xsl!="") {
			$output.="<?xml-stylesheet type=\"text/css\" href=\"".$this->xsl."\"?>";
		} 
		
		$output.="<rss version=\"".$this->rssFormat."\">\n";
		$output.="   <channel>\n";
		$output.="       <title>".$this->title."</title>\n";
		$output.="       <link>".$this->link."</link>\n";
		$output.="       <description>".$this->description."</description>\n";
		$output.="       <language>".$this->language."</language>\n";
		$output.="       <lastBuildDate>".$this->get_date("")."</lastBuildDate>\n";
		$output.="       <generator>".$this->generator."</generator>\n";
		$output.=$this->image_data;
		for($i=0;$i<count($this->items);$i++) {
			$output.="      <item>\n";
				$output.="          <title>".$this->items[$i]->title."</title>\n";
				$output.="          <description>".htmlspecialchars($this->items[$i]->description)."</description>\n";
				$output.="          <link>".$this->items[$i]->link."</link>\n";
				$output.="          <pubDate>".$this->get_date($this->items[$i]->pubDate)."</pubDate>\n";
				if($this->items[$i]->category!="")
				$output.="          <category>".$this->items[$i]->category."</category>\n";
				if($this->items[$i]->author!="")
				$output.="          <author>".$this->items[$i]->author."</author>\n";
				if($this->items[$i]->guid!="")
				$output.="          <guid>".$this->items[$i]->guid."</guid>\n";
			$output.="      </item>\n";
		}
		
		$output.="   </channel>\n";
		$output.="</rss>\n";
		return $output;
	}
	
	function save_feed($file_name=""){
		if($file_name=="")
			$file_name="rss.xml";
		$feedFile=fopen($file_name,"w+");
		if($feedFile) {
			fputs($feedFile,$this->create_feed());
			fclose($feedFile);
			$this->redirect($file_name);
		} else {
			echo "Error Creating File";
		}
	}
	
	
}

class FeedItem{
	public $title="";
	public $description="";
	public $link="";
	public $guid="";
	public $pubDate="";
	public $category="";
	public $author="";
		
}

$Rss=new FeedCreate;
$Rss->add_header("This is title", "http://news.bbc.co.uk/go/rss/-/2/hi/default.stm", "This is description.");
$Rss->create_image("BBC News", "http://localhost/rss/logo.png", "http://localhost/rss/");


	$item=new FeedItem;
	$item->title="This is title";
	$item->link="http://surajthapaliya.com.np";
	$item->pubDate=time();
	$item->category="Europe";
	$item->description="<b>This is description</b>";
	$item->author="Suraj Thapaliya";
	
$Rss->add_items($item);
echo $Rss->create_feed();
	
?>